package com.guigu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.mapper.OrderPaymentMapper;
import com.guigu.mapper.PurchaseOrderDetailsMapper;
import com.guigu.mapper.PurchaseOrderMapper;
import com.guigu.pojo.Orders;
import com.guigu.pojo.Product;
import com.guigu.pojo.OrderPayment;
import com.guigu.pojo.Product;
import com.guigu.pojo.PurchaseOrder;
import com.guigu.pojo.PurchaseOrderDetails;
import com.guigu.service.ProductService;
import com.guigu.service.PurchaseOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
/**
 * <p>
 * 采购单表 服务实现类
 * </p>
 *
 * @author Mr.Zhang
 * @since 2025-03-12
 */
@Service
public class PurchaseOrderServiceImpl extends ServiceImpl<PurchaseOrderMapper, PurchaseOrder> implements PurchaseOrderService {

    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    @Override
    public PurchaseOrder getEndPurchaseOrder() {

        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("purchase_id");
        queryWrapper.last("limit 1");
        PurchaseOrder purchaseOrder = getOne(queryWrapper);
        System.out.println(purchaseOrder);
        return purchaseOrder;
    }

    @Override
    public int addPurchaseOrder(PurchaseOrder purchaseOrder) {
        //返回主键
        int insert = purchaseOrderMapper.insert(purchaseOrder);
        return purchaseOrder.getPurchaseId();
    }

    @Override
    public PurchaseOrder getPurchaseOrderBySupplierId(Integer id) {
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",id);
        queryWrapper.eq("check_tag","S001-1");
        PurchaseOrder purchaseOrder = getOne(queryWrapper);
        if (purchaseOrder!=null){
            return purchaseOrder;
        }

        return null;
    }

    //审核
    @Override
    public List<PurchaseOrder> getPurchaseReview(HashMap<String, Object> map) {
        // 判断如果map.get("checkTag")有值，就将map.get("CheckTag")的值设为空字符串
        if (map.get("checkTag") != null && StringUtils.isNotBlank((String) map.get("checkTag"))) {
            map.put("CheckTage", "");
        }
        System.out.println(map);
        List<PurchaseOrder> purchaseReview = purchaseOrderMapper.purchaseReview(map);
        System.out.println(purchaseReview);
        return purchaseReview;
    }

    @Override
    public PurchaseOrder getPurchaseOrderBypurchaseOrderNumber(String purchaseOrderNumber) {
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("purchase_order_number",purchaseOrderNumber);
        PurchaseOrder purchaseOrder = purchaseOrderMapper.selectOne(queryWrapper);
        System.out.println(purchaseOrder);
        return purchaseOrder;
    }

    @Override
    public List<PurchaseOrder> selectAllPurchaseOrder(HashMap<String, Object> map) {
        List<PurchaseOrder> purchaseOrders = purchaseOrderMapper.purchaseList(map);
        System.out.println(purchaseOrders);

        return purchaseOrders;
    }

    @Autowired
    PurchaseOrderDetailsMapper purchaseOrderDetailsMapper;

    @Autowired
    OrderPaymentMapper orderPaymentMapper;

    @Autowired
    ProductService productService;


    @Override
    public Page<PurchaseOrder> getSupplierOrderBySupplierId(Integer supplierId, Integer currentPage, Integer pageSize) {
        Page<PurchaseOrder> page = new Page<>(currentPage, pageSize);
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",supplierId);
        queryWrapper.orderByDesc("register_time");
        queryWrapper.eq("check_tag","S001-3");
        Page<PurchaseOrder> purchaseOrderPage = purchaseOrderMapper.selectPage(page, queryWrapper);
        List<PurchaseOrder> purchaseOrders = purchaseOrderPage.getRecords();
        if(purchaseOrders != null){
            for(PurchaseOrder purchaseOrder : purchaseOrders){
                String purchaseOrderNumber = purchaseOrder.getPurchaseOrderNumber();

                QueryWrapper<OrderPayment> queryWrapper1 = new QueryWrapper<>();
                queryWrapper1.eq("order_id",purchaseOrderNumber);
                OrderPayment orderPayment = orderPaymentMapper.selectOne(queryWrapper1);
                if(orderPayment.getPaymentStatus().equals("K002-3")){
                    QueryWrapper<PurchaseOrderDetails> queryWrapper2 = new QueryWrapper<>();
                    queryWrapper2.eq("purchase_id",purchaseOrder.getPurchaseId());
                    List<PurchaseOrderDetails> purchaseOrderDetails = purchaseOrderDetailsMapper.selectList(queryWrapper2);
                    if(purchaseOrderDetails != null){
                        for(PurchaseOrderDetails purchaseOrderDetail : purchaseOrderDetails){
                            Integer productId = purchaseOrderDetail.getProductId();
                            Product productById = productService.getProductById(productId);
                            purchaseOrderDetail.setProduct(productById);
                        }
                    }
                    purchaseOrder.setPurchaseOrderDetails(purchaseOrderDetails);
                }else {
                    purchaseOrders.remove(purchaseOrder);
                }
                purchaseOrderPage.setRecords(purchaseOrders);
            }
        }

        return purchaseOrderPage;
    }

    @Override
    public Integer getSupplierOrderCountBySupplierId(Integer supplierId) {
        QueryWrapper<PurchaseOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("supplier_id",supplierId);
        queryWrapper.eq("check_tag","S001-3");
        Integer count = purchaseOrderMapper.selectCount(queryWrapper);
        if(count != null){
            return count;
        }
        return 0;
    }
}
